package com.guo.hui.controller;

import com.alibaba.fastjson.JSONObject;
import com.guo.hui.entity.MyUser;
import com.guo.hui.util.MyException;
import com.guo.hui.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author zhengguohui
 * @Date 2018/8/31 16:30
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;



    @RequestMapping("/home")
    public String home() {
        return "home";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public String getUserList(@RequestBody String param) {
        JSONObject object = getJsonObject(param);
        List<MyUser> list = userService.getUserList(object);
        return JSONObject.toJSONString(list);
    }


    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public String info(@RequestBody String param) {
        JSONObject object = getJsonObject(param);
        MyUser user = userService.getMyUser(object);
        return JSONObject.toJSONString(user);
    }

    @RequestMapping("/add")
    public String add(String param) {
        return "add";
    }

    @RequestMapping("/edit")
    public String edit(String param) {
        return "edit";
    }

    @RequestMapping("/delete")
    public String delete(String param) {
        return "";
    }

    /**
     * @Author zhengguohui
     * @Description 参数转化字符串包装JSONObject对象
     * @Date 2018/8/31 17:44
     * @Param [param]
     * @Return com.alibaba.fastjson.JSONObject
     */
    private JSONObject getJsonObject(String param) {
        try {
            JSONObject object = JSONObject.parseObject(param);
            return object;
        } catch (Exception e) {
            throw new MyException("参数不是JSON格式,请核对参数");
        }
    }


}
